package com.zenithmind.note.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.note.pojo.dto.NoteShareDTO;
import com.zenithmind.note.pojo.entity.NoteShare;
import com.zenithmind.note.pojo.query.NoteShareQuery;
import com.zenithmind.note.pojo.query.UserShareQuery;
import com.zenithmind.note.pojo.vo.NoteShareVO;
import com.zenithmind.note.pojo.vo.NoteVO;
import com.zenithmind.note.service.NoteShareService;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 笔记分享控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/note-share")    
@RequiredArgsConstructor
@Tag(name = "笔记分享管理")
public class NoteShareController {
    
    private final NoteShareService noteShareService;
    
    /**
     * 创建分享
     */
    @Log(description = "创建笔记分享", operationType = OperationTypeEnum.ADD)
    @PostMapping("/createNoteShare")
    @Operation(summary = "创建分享")
    public Result<NoteShareVO> createShare(@Validated @RequestBody NoteShareDTO noteShareDTO) {
        log.info("创建分享：{}", noteShareDTO);
        return Result.success(noteShareService.createShare(noteShareDTO));
    }
    
    /**
     * 获取分享详情
     */
    @Log(description = "获取笔记分享详情", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getNoteShareDetail/{shareId}")
    @Operation(summary = "获取分享详情")
    public Result<NoteShareVO> getShareDetail(@PathVariable String shareId) {
        log.info("获取分享详情：{}", shareId);
        return Result.success(noteShareService.getShareDetail(shareId));
    }
    
    /**
     * 通过分享码查看笔记
     */
    @Log(description = "通过分享码查看笔记", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/viewSharedNote/{shareCode}")
    @Operation(summary = "通过分享码查看笔记")
    public Result<NoteVO> viewSharedNote(@PathVariable String shareCode) {
        log.info("通过分享码查看笔记：{}", shareCode);
        return Result.success(noteShareService.getNoteByShareCode(shareCode));
    }
    
    /**
     * 取消分享
     */
    @Log(description = "取消笔记分享", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/cancelNoteShare/{shareId}")
    @Operation(summary = "取消分享")
    public Result<Boolean> cancelShare(@PathVariable String shareId) {
        log.info("取消分享：{}", shareId);
        return noteShareService.cancelShare(shareId);
    }
    
    /**
     * 获取用户分享列表
     */
    @Log(description = "获取用户笔记分享列表", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getUserShares")
    @Operation(summary = "获取用户分享列表")
    public Result<PageResult<NoteShareVO>> getUserShares(UserShareQuery query) {
        log.info("获取用户分享列表：{}", query);
        return Result.success(noteShareService.getUserSharePage(query));
    }
    
    /**
     * 分页查询分享记录（管理功能）
     */
    @Log(description = "分页查询笔记分享记录", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/shareManagePage")
    @Operation(summary = "分页查询分享记录-管理功能")
    public Result<PageResult<NoteShareVO>> shareManagePage(NoteShareQuery query) {
        log.info("管理端分页查询分享记录：{}", query);
        return Result.success(noteShareService.shareManagePage(query));
    }
    
    /**
     * 获取分享详情（管理功能）
     */
    @Log(description = "获取分享详情", operationType = OperationTypeEnum.QUERY)
    @GetMapping("/getShareById")
    @Operation(summary = "获取分享详情-管理功能")
    public Result<NoteShareVO> getShareById(@RequestParam("shareId") String shareId) {
        log.info("管理端获取分享详情：{}", shareId);
        return Result.success(noteShareService.getShareById(shareId));
    }

    /**
     * 添加或修改分享记录（管理功能）
     */
    @Log(description = "添加或修改分享记录", operationType = OperationTypeEnum.UPDATE)
    @PostMapping("/addOrUpdateShare")
    @Operation(summary = "添加或修改分享记录-管理功能")
    public Result<NoteShareVO> addOrUpdateShare(@RequestBody NoteShare share) {
        log.info("添加或修改分享记录：{}", share);
        return Result.success(noteShareService.addOrUpdateShare(share));
    }
    
    /**
     * 批量删除分享记录（管理功能）
     */
    @Log(description = "批量删除分享记录", operationType = OperationTypeEnum.DELETE)
    @DeleteMapping("/deleteBatchShares")
    @Operation(summary = "批量删除分享记录-管理功能")
    public Result<Boolean> deleteBatchShares(@RequestBody List<String> ids) {
        log.info("批量删除分享记录：{}", ids);
        noteShareService.deleteBatch(ids);
        return Result.success(true);
    }
    
    /**
     * 批量更新分享状态（管理功能）
     */
    @Log(description = "批量更新分享状态", operationType = OperationTypeEnum.UPDATE)
    @PutMapping("/updateBatchShareStatus")
    @Operation(summary = "批量更新分享状态-管理功能")
    public Result<Void> batchUpdateStatus(@RequestParam String ids, @RequestParam Integer status) {
        log.info("批量更新分享状态：ids={}, status={}", ids, status);
        List<String> idList = java.util.Arrays.stream(ids.split(","))
                .collect(java.util.stream.Collectors.toList());
        noteShareService.batchUpdateStatus(idList, status);
        return Result.success();
    }
} 